package com.codingclub.domain.handle.subject;

import com.codingclub.common.enums.ErrCode;
import com.codingclub.common.enums.SubjectInfoTypeEnum;
import com.codingclub.common.exception.BusinessException;
import com.codingclub.domain.bo.SubjectInfoBO;
import com.codingclub.infra.basic.entity.SubjectBrief;
import com.codingclub.infra.basic.service.SubjectBriefService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

/**
 * 简答题目处理策略
 */
@Component
public class BriefTypeHandler implements SubjectTypeHandler{
    private final SubjectBriefService subjectBriefService;
    //构造器注入
    public BriefTypeHandler(SubjectBriefService subjectBriefService){
        this.subjectBriefService=subjectBriefService;
    }
    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.BRIEF;
    }

    @Override
    public boolean add(SubjectInfoBO bo) {
        Long id = bo.getId();
        String subjectAnswer = bo.getSubjectAnswer();
        if(id==null|| StringUtils.isBlank(subjectAnswer)){
            throw new BusinessException(ErrCode.PARAMS_ERROR);
        }
        SubjectBrief subjectBrief = new SubjectBrief();
        subjectBrief.setId(id);
        subjectBrief.setSubjectAnswer(subjectAnswer);
        return subjectBriefService.insert(subjectBrief);
    }
}
